<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="GuideList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="语言种类" align="center" prop="language">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.language" :value="scope.row.language"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleDetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="mNotice_form">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="语言种类" prop="language">
          <el-select v-model="form.language" placeholder="请选择语言种类">
            <el-option
              v-for="dict in dict.type.language"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="showDetails" width="700px" append-to-body>
      <el-form ref="formDetails" :model="formDetails" :rules="rulesDetails" label-width="80px" class="mNotice_form">
        <el-form-item label="内容" prop="content">
          <editor v-model="formDetails.content" :min-height="400"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDetailsForm">确 定</el-button>
        <el-button @click="cancelDetails">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuide, delGuide, addGuide, updateGuide, detailsGuide } from "@/api/system/beginnerGuide";

export default {
  name: "beginnerGuide",
  dicts: [ 'language'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 币币-公告表格数据
      GuideList: [],
      // 弹出层标题
      title: "",
      titleDetails: '',
      // 是否显示弹出层
      open: false,
      showDetails: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        language: [
          { required: true, message: "请选择语言种类", trigger: "blur" }
        ],
      },
      // 表单参数
      formDetails: {},
      rulesDetails: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询币币-公告列表 */
    getList() {
      this.loading = true;
      listGuide(this.queryParams).then(response => {
        console.log(response)
        this.GuideList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.title = row.title;
      this.form.id = row.id;
      this.open = true;
      this.title = "修改标题";
    },
    /** 提交按钮 */ 
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGuide(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuide(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    detailsReset() {
      this.formDetails = {
        id: null,
        content: null,
      };
      this.resetForm("formDetails");
    },
    /** 详情 */
    handleDetails(row) {
      this.detailsReset();
      const id = row.id;
      detailsGuide(id).then(response => {
        this.formDetails = response.data;
        this.showDetails = true;
        this.titleDetails = "详情";
      });
    },
    // 取消按钮
    cancelDetails() {
      this.showDetails = false;
      this.detailsReset();
    },
    submitDetailsForm() {
      this.$refs["formDetails"].validate(valid => {
        if (valid) {
          updateGuide(this.formDetails).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.showDetails = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除参数编号为"' + row.id + '"的数据项？').then(function() {
        return delGuide({id: row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>